package com.example.demo.service;

import com.example.demo.entity.Result;
import com.example.demo.entity.pojo.*;
import com.example.demo.entity.vo.StudentVo;
import com.example.demo.exception.LoginException;
import com.example.demo.mapper.BookMapper;
import com.example.demo.mapper.CardMapper;
import com.example.demo.mapper.StudentMapper;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    CostRecordService costRecordService;
    @Autowired
    RecordService recordService;
    @Autowired
    BookMapper bookMapper;

    @Autowired
    CardService cardService;

    /**
     * 登录
     *
     * @param account
     * @return
     */
    public Student login(String account) {
        return studentMapper.login(account);
    }

    /**
     * 查看个人信息
     *
     * @return
     */
    public StudentVo getMessage(int id) throws LoginException {
        Student student = studentMapper.getMessage(id);
        if (student == null) {
            throw new LoginException("学生信息为空");
        }
        //根据卡的id差等级id 联表查
        List<LevelType> levelType = studentMapper.getType(student.getCardId());
        if (levelType == null) {
            return null;
        }
        //获取卡的信息
        Card card = studentMapper.getCardMessage(student.getId());
        if (card == null) {
            throw new LoginException("卡的信息为空");
        }
        StudentVo studentVo = new StudentVo(student, levelType, card);
        return studentVo;
    }


    /**
     * 充值
     *
     * @param id 学生id
     * @return
     */
    public int recharge(int id, double money) {
        Card card = studentMapper.getCardMessage(id);
        boolean b = costRecordService.addCostRecord(money, card.getId(),0);
        if (!b) {
            return 0;
        }
        double oldMoney = card.getMoney();
        double newMoney = money + oldMoney;
        return studentMapper.recharge(newMoney, id);
    }

    /**
     * 消费
     *
     * @param id
     * @param money
     * @return
     */
    public boolean consumption(int id, double money) {
        return studentMapper.consumption(money, id) > 0;
    }

    /**
     * 借书
     *
     * @param cardId 卡的id
     * @param bookId
     * @return
     */
    public boolean borrowBook(int cardId, int bookId) throws LoginException {
        if (recordService.borrowBook(cardId, bookId)) {
            Book bookMessage = bookMapper.getBookMessage(bookId);
            //修改图书数量
            int i1 = bookMapper.changeBookState(bookId, bookMessage.getCount()-1);
            if (i1 > 0) {
                //借成功
                //TODO 可借次数减一
                Card card = studentMapper.getCardMessage(cardId);
                int num = card.getCount() - 1;
                int count = studentMapper.changeCardCount(num, cardId);
                return count > 0;
            }
        }
        return false;
    }


    /**
     * 修改密码
     *
     * @param id          id
     * @param newPassword 新密码
     * @param oldPassword 旧密码
     * @return
     */
    public boolean modifyPassword(int id, String newPassword, String oldPassword) {
        int i = studentMapper.modifyPassword(id, newPassword, oldPassword);
        return i > 0;
    }

    /**
     * 归还书籍
     *
     * @param cid 卡id
     * @param id  书籍id
     * @return
     */
    @Transactional
    public String returnBook(int cid, int id) {
        BookRecord bookById = recordService.getBookById(id);
        if (bookById == null) {
            return "没有此纪录";
        }
        Date returnTime = bookById.getReturnTime();
        Date d = new Date();
        if (returnTime.getTime() > d.getTime()) {
            //正常归还
            boolean b = recordService.returnBook(id);
            if (b) {
                Book bookMessage = bookMapper.getBookMessage(id);
                int i = bookMapper.changeBookState(id, bookMessage.getCount()+1);
                if (i > 0) {
                    Card cardMessage = studentMapper.getCardMessage(cid);
                    int count = studentMapper.changeCardCount(cardMessage.getCount() + 1, cid);
                    if (count < 0) {
                        return "归还失败";
                    }
                    return "归还成功";
                }
            }
            return "归还失败";
        }
        //扣费归还
        double money = bookMapper.getBookMessage(id).getPrice() * 0.15;
        Card cardMessage = studentMapper.getCardMessage(cid);
        if (costRecordService.addCostRecord(money * -1, id,2)) {
            if (this.consumption(cid, cardMessage.getMoney() - money)) {
                if (recordService.returnBook(id)) {
                    int i = bookMapper.changeBookState(id, 1);
                    if (i > 0) {
                        int count = studentMapper.changeCardCount(cardMessage.getCount() + 1, cid);
                        if (count < 0) {
                            return "归还失败";
                        }
                        return "延期归还 扣除书费的15%";
                    }
                }
            }
            return "归还失败 延期归还 余额不足";
        }
        return "归还失败";
    }

    /**
     * @param account
     * @param password
     * @param sex
     * @param age
     * @param name
     * @param graderNumber
     * @return
     */
    @Transactional
    public boolean register(String account, String password, String sex, String age, String name, String graderNumber) {
        int regiser = studentMapper.register(account, password, sex, age, name, graderNumber);
        Student messageByAccount = getMessageByAccount(account, graderNumber);
        if (messageByAccount == null){
            return false;
        }
        if (cardService.addCard(messageByAccount.getId(),graderNumber)){
            //卡的信息添加成功
            //查询卡的id
            Card cardMessage = studentMapper.getCardMessage(messageByAccount.getId());
            int i = studentMapper.addCardId(messageByAccount.getId(), cardMessage.getId());
            return i > 0;
        }
        return false;

    }

    /**
     * 根据账号查询是否能注册
     *
     * @param account
     * @return
     */
    public Student getMessageByAccount(String account, String gardeNumber) {
        Student messageByAccount = studentMapper.getMessageByAccount(account, gardeNumber);
        return messageByAccount;
    }
}
